<?php

class administration_Controller extends Controller {
    
    public function _default($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('menu',$params);
        return $view;
    }
    
    public function accounts($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('accounts',$params);
        return $view;
    }
    
    public function createUser($params) {
        $view = new administration_View(new Module((int)$params['module']));
        
        if(isset($_POST) && count($_POST)) {
            //createUser form sent
            $user = new AnyUser(0);
            $user->setUserData($_POST);
            if(($uid = $user->create())) {
                $params['uid'] = $uid;
                $view->createView('createUserSuccess',$params);
            } else {
                $view->createView('createUserError',array_merge($params,array('userInfo'=>$_POST)));
            }
        }
        else {
            $view->createView('createUser', $params);
        }
        
        return $view;
    }
    
    public function delUser($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if(isset($_POST) && count($_POST)) {
            $user = new AnyUser((int)$_POST['uid']);
            if($user->deleteUserFromDB()) {
                $view->createView('deleteUserSuccess', array_merge($params,array('uid'=>$_POST['uid'],'details'=>$_POST['details'])));
            } else {
                $view->createView('deleteUserError', array_merge($params,array('uid'=>$_POST['uid'],'details'=>$_POST['details'])));
            }
        } else {
            $view->createView('deleteUser',$params);
        }
        return $view;
    }
    
    public function editUser($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if(isset($_POST['commit']) && isset($_POST['uid'])) {
            $user = new AnyUser((int)$_POST['uid']);
            $user->setUserData($_POST);
            if($user->updateUserInfoInDB()) {
                $view->createView('editUserSuccess', array_merge($params,array('uid'=>$_POST['uid'])));
            } else {
                $view->createView('editUserError', array_merge($params,array('uid'=>$_POST['uid'])));
            }
        } else {
            $view->createView('editUser',$params);
        }
        return $view;
    }
}

?>
